import os.path

import pyautogui
from mon.baselib.utils import TimeUtils

from src.handler.hongbao import HongBaoConfig
from src.handler.hongbao.AbstractProcess import AbstractProcess
from src.utils import PyautoguiUtils


def check_ji_xu_xin_shang(sub_image_path: str = "", save_path: str = "") -> dict:
    result = PyautoguiUtils.find_image_position(sub_image_path, 668, 540, 535, 300, save_path=save_path)
    found = result["found"]
    result["completed"] = found
    return result


class TecentJiXuXinShangProcess(AbstractProcess):
    def __init__(self, config, config_dict):
        super().__init__(config, config_dict)
        self.config = config
        self.config_dict = config_dict

    def check(self) -> dict:
        sub_image_path = os.path.join(self.config.resource_path(), "ji_xu_xin_shang.png")
        save_path = HongBaoConfig.get_config_dir_file("cap_ji_xu_xin_shang.png")
        result = TimeUtils.check_task_completion(check_ji_xu_xin_shang, sub_image_path=sub_image_path, timeout=75, save_path=save_path)
        completed = result["completed"]
        result["check"] = completed
        return result

    def do_process(self, check_result: dict):
        pyautogui.moveTo(check_result['x'], check_result['y'], duration=1)
        pyautogui.click(check_result['x'], check_result['y'])
        return True

    def do_not_check(self, check_result: dict) -> bool:
        return False
